package config

import (
	"github.com/spf13/viper"
)

// Config 定义应用程序的配置结构
type Config struct {
	Server   ServerConfig   `mapstructure:"server"`
	Binance  BinanceConfig  `mapstructure:"binance"`
	Logging  LoggingConfig  `mapstructure:"logging"`
}

// ServerConfig 定义WebSocket服务器的配置
type ServerConfig struct {
	Port            int    `mapstructure:"port"`
	ReadTimeout     string `mapstructure:"read_timeout"`
	WriteTimeout    string `mapstructure:"write_timeout"`
	IdleTimeout     string `mapstructure:"idle_timeout"`
	MaxMessageSize  int    `mapstructure:"max_message_size"`
}

// BinanceConfig 定义币安WebSocket连接的配置
type BinanceConfig struct {
	WSURL            string   `mapstructure:"ws_url"`
	MaxConnections   int      `mapstructure:"max_connections"`
	RetryInterval    string   `mapstructure:"retry_interval"`
	SubscriptionTypes []string `mapstructure:"subscription_types"`
	Symbols          []string `mapstructure:"symbols"`
	KlineIntervals   []string `mapstructure:"kline_intervals"`
}

// LoggingConfig 定义日志配置
type LoggingConfig struct {
	Level  string `mapstructure:"level"`
	Format string `mapstructure:"format"`
}

// LoadConfig 加载配置文件
func LoadConfig(configPath string) (*Config, error) {
	viper.SetConfigFile(configPath)

	// 读取配置文件
	if err := viper.ReadInConfig(); err != nil {
		return nil, err
	}

	// 监听配置文件变化
	viper.WatchConfig()

	// 解析配置
	var config Config
	if err := viper.Unmarshal(&config); err != nil {
		return nil, err
	}

	return &config, nil
}